package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DataMaterialBatch;
import com.uwlaser.service.DataMaterialBatchService;
import com.uwlaser.service.dto.DataMaterialBatchDto;
import com.uwlaser.service.dto.DataMaterialBatchQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "物料批次管理")
@RequestMapping("/api/dataMaterialBatch")
public class DataMaterialBatchController {

    private final DataMaterialBatchService dataMaterialBatchService;

    @GetMapping
    @Log("查询物料批次")
    @ApiOperation("查询物料批次")
    @PreAuthorize("@el.check('dataMaterialBatch:list')")
    public ResponseEntity query(DataMaterialBatchQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataMaterialBatchService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增物料批次")
    @ApiOperation("新增物料批次")
    @PreAuthorize("@el.check('dataMaterialBatch:add')")
    public ResponseEntity create(@Validated @RequestBody DataMaterialBatchDto resources){
        return new ResponseEntity<>(dataMaterialBatchService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改物料批次")
    @ApiOperation("修改物料批次")
    @PreAuthorize("@el.check('dataMaterialBatch:edit')")
    public ResponseEntity update(@Validated @RequestBody DataMaterialBatchDto resources){
        dataMaterialBatchService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除物料批次")
    @ApiOperation("删除物料批次")
    @PreAuthorize("@el.check('dataMaterialBatch:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        dataMaterialBatchService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出物料批次")
    @ApiOperation("导出物料批次")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataMaterialBatch:list')")
    public void download(HttpServletResponse response, DataMaterialBatchQueryParam query) throws IOException {
        dataMaterialBatchService.download(dataMaterialBatchService.queryAll(query), response);
    }

}
